/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package physical_spaces.beans;

import java.sql.SQLException;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.RowEditEvent;
import physical_spaces.DAO.PhysicalSpaceDAO;
import physical_spaces.business.GlobalVariable;
import physical_spaces.business.GlobalVariablesBusiness;

/**
 *
 * @author JuanFelipe
 */
@ManagedBean(name = "tablePhysical")
@ViewScoped
public class TablePhysicalBean {
    
    private List<GlobalVariable> globalVariables;
    GlobalVariable gb;

    public GlobalVariable getGb() {
        return gb;
    }

    public void setGb(GlobalVariable gb) {
        this.gb = gb;
    }
    
    public List<GlobalVariable> getGlobalVariables() {
        return globalVariables;
    }

    public void setGlobalVariables(List<GlobalVariable> globalVariables) {
        this.globalVariables = globalVariables;
    }

    public TablePhysicalBean() {
        GlobalVariablesBusiness ob;
        
        try {
            ob = new GlobalVariablesBusiness(new PhysicalSpaceDAO().getGlobalVariables());
            globalVariables = ob.getGlobalVariables();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    
    public void onEdit(RowEditEvent event) {  
        FacesMessage msg = new FacesMessage("Car Edited", /*((Car) event.getObject()).getModel()*/"...");  
        System.out.print("EDIT!!!!! >>>");
        gb = ((GlobalVariable) event.getObject());
        System.out.println("NAME: "+gb.getVariableName()+" VAL: "+gb.getVariableValue() );
        GlobalVariablesBusiness ob;
        
        ob = new GlobalVariablesBusiness();
        
        if ( !ob.setValue( gb.getVariableName(), gb.getVariableValue() ) ){
            System.out.println("NO GOOD");
            onCancel(event);
            return;
        }
        System.out.println("GOOD");
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  
      
    public void onCancel(RowEditEvent event) {  
        FacesMessage msg = new FacesMessage("Car Cancelled", /*((Car) event.getObject()).getModel()*/"...");  
        System.out.println("CAncel");
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  
    
}
